import * as echarts from '../../../components/ec-canvas/echarts';
const api = require("../../../apis/modules/staff")

let chart = null;
let chart2 = null;
let tempData =[]

// 2、进行初始化数据
function initChart2(canvas, width, height) {
  chart2 = echarts.init(canvas, null, {
    width: width,
    height: height
  });
  canvas.setChart(chart2);

  var option = {
    title: {
      text: "体温检测结果统计",
      left: "left"
    },
    tooltip: {
      trigger: "item",
      formatter: "{a} <br/>{b} : {c} ({d}%)"
    },
    legend: {
      orient: "vertical",
      right: "right",
      data: ["小于36.6℃", "36.6~37.5℃", "大于37.5℃"]
    },
    series: [
      {
        name: "访问来源",
        type: "pie",
        radius: "55%",
        center: ["50%", "60%"],
        data:[
          { value: 335, name: "小于36.6℃" },
          { value: 234, name: "36.6~37.5℃" },
          { value: 135, name: "大于37.5℃" }
        ],
        emphasis: {
          itemStyle: {
            shadowBlur: 10,
            shadowOffsetX: 0,
            shadowColor: "rgba(0, 0, 0, 0.5)"
          }
        }
      }
    ]
  };

  chart2.setOption(option);
  return chart2;
}
// 2、进行初始化数据
function initChart(canvas, width, height) {
  chart = echarts.init(canvas, null, {
    width: width,
    height: height
  });
  canvas.setChart(chart);

  var option = {
    title: {
      text: "近一周检测次数结果统计"
    },
    tooltip: {
      trigger: "axis"
    },
    grid: {
      left: "3%",
      right: "4%",
      bottom: "3%",
      containLabel: true
    },
    toolbox: {
      feature: {
        saveAsImage: {}
      }
    },
    xAxis: {
      type: "category",
      boundaryGap: false,
      data: ["周一", "周二", "周三", "周四", "周五", "周六", "周日"]
    },
    yAxis: {
      type: "value",
      name: "次数"
    },
    series: [
      {
        name: "进入社区",
        type: "line",
        stack: "总量",
        data: [120, 132, 101, 134, 90, 230, 210]
      },
      {
        name: "离开社区",
        type: "line",
        stack: "总量",
        data: [220, 182, 191, 234, 290, 330, 310]
      }
    ]
  };

  chart.setOption(option);
  return chart;
}

Page({
  onShareAppMessage: function (res) {
    return {
      title: 'ECharts',
      path: '/pages/index/index',
      success: function () { },
      fail: function () { }
    }
  },
  data: {
    totalSuspected: "",
    totalConfirmed: "",
    totalHousehold: "",
    tempData: [],
    ec: {
      onInit: initChart // 3、将数据放入到里面
    },
    ec2:{
      onInit: initChart2
    }
  },

  onLoad(){
    api.accessStatistic().then(res=>{
   if(res.resultCode ==0){
     this.setData({
      totalHousehold : res.data.totalHousehold,
      totalConfirmed : res.data.totalConfirmed,
      totalSuspected : res.data.totalSuspected
     })
     let option = chart.getOption();
     console.log(option)
     option.xAxis.data= res.data.household.x_time
     option.series[0].data = []
     option.series[1].data = []
     for(var item of res.data.household.chartData ){
      option.series[0].data.push(item.y_value[0])
      option.series[1].data.push(item.y_value[1])
     }
     console.log(option)
     chart.setOption(option, true);
   }
    })
    api.tempData().then(res=>{
      var option = chart2.getOption();
   
      tempData[1] = {
       name: "36.6~37.5℃",
       value: res.data.normal
     };
     tempData[2] = {
       name: "大于37.5℃",
       value: res.data.over
     };
     tempData[0] = {
       name: "小于36.6℃",
       value: res.data.under
     };
     option.series[0].data = tempData
     chart2.setOption(option, true);
    })
  }
});

